<?php

/**
 *  228
 */
require_once('util/txt2array.php');
require_once('util/array2csv.php');
require_once('util/QuestionDO.php');

// opening a directory
$dir = "util/txt/";
$dir_handle = opendir($dir);

$headers = ["char", "char2"];
// reading the contents of the directory
while (($file_name = readdir($dir_handle)) !== false) {
    $file_name = str_replace(["txt", "."], "", $file_name);
    if (!empty($file_name)) {
        $paper = txt2Array("util/txt/{$file_name}.txt");
        if (!empty($paper)) {
            $data = convertTxtCsv($paper);
            if (empty($data["targetName"])) {
                echo "没有目标文件名：" . $file_name;
            }
            $file_name = "util\csv1\\" . $data["targetName"];
            toCSV($file_name, $data["record"], getHeaders());
        }
    }
}
// closing the directory
closedir($dir_handle);

function convertTxtCsv($paper)
{
    if (empty($paper)) {
        return;
    }
    $data = [
        "targetName" => "",
        "record" => [],
    ];
    $courseCodeMap = [
        "康复工程学" => '="00202001"',
        "康复评定学" => '="00228001"',
        "临床基础检验学技术" => '="00229001"',
        "临床医学概论" => '="00215001"',
        "内经选读" => '="00237001"',
        "伤寒论" => '="00149001"',
        "生物化学" => '="00058001"',
        "推拿学" => '="00171001"',
        "外科护理学" => '="00008001"',
        "西医内科学" => '="00035001"',
        "西医外科学" => '="00039001"',
        "药剂学" => '="00094001"',
        "药理学" => '="00012001"',
        "药事管理与法规" => '="00015001"',
        "药物化学" => '="00049001"',
        "药用植物学" => '="00054001"',
        "医学统计学" => '="00055001"',
        "医学影像检查技术学" => '="00221001"',
        "医学影像设备学" => '="00232001"',
        "运动疗法技术学" => '="00225001"',
        "针灸学" => '="00034001"',
        "诊断学基础" => '="00165001"',
        "中药炮制学" => '="00048001"',
        "中药药剂学" => '="00095001"',
        "中药药理学" => '="00033001"',
        "中医护理学基础" => '="00162001"',
        "中医内科学" => '="00028001"',
        "作业治疗学" => '="00226001"',
    ];


    $paper = array_values(array_filter($paper, "trim"));

    $type = ""; //题型
    $answerStrAll = "";   //答案
    $score = "0";
    $courseCode = '=""';                                                      //4
    $QA = "Q";


    $questionMap = [new QuestionDO()];
//处理data
    foreach ($paper as $item) {
        $item = trim($item);
        $item = str_replace("\t", "", $item);

//    输出文件名
        if (preg_match("/(试题)$/", $item)) {
            preg_match("/(《).*(》)/", $item, $csvNames);
            if (!empty($csvNames)) {
                $csvName = str_replace(["《", "》"], "", $csvNames[0]);
                $data["targetName"] = "2021级2022秋" . $csvName . "期末试卷";
                $courseCode = $courseCodeMap[$csvName];
            }
        }

//    题型，分数
        if (preg_match("/(单)(项)?(选).*(题)/", $item)) {
            $type = "单选题";
            $score = getScore($item);
            if (empty($score)) {
                $score = 1;
            }
            continue;
        }
        if (preg_match("/(多)(项)?(选).*(题)/", $item)) {
            $type = "多选题";
            $score = getScore($item);
            if (empty($score)) {
                $score = 2;
            }
            continue;
        }
        if (strpos($item, "判断题") !== false) {
            $type = "判断题";
            $score = getScore($item);
            if (empty($score)) {
                $score = 1;
            }
            continue;
        }

        if (strpos($item, "标准答案") !== false || strpos($item, "参考答案") !== false) {
            $QA = "A";
            continue;
        }
        if ($QA === "Q") {
            $questionMap = createQuestion($item, $type, $score, $questionMap);
            $currentQuestion = end($questionMap);
            setOperation($item, $currentQuestion);
            if (strpos($item, "正确答案") === 0) {
                $currentQuestion->setAnswer(getAnswerFromStem($item));
            }
        } else if ($QA === "A") {
//    答案
            if (preg_match("/^[0-9]{1,3}/", $item)) {
                $answerStrAll .= $item;

            }
        }
    }
    setAnswerArray($answerStrAll, $questionMap);

    foreach ($questionMap as $number => $question) {
        if (!empty($question->getStem())) {
            if ($question->getType() === "判断题") {
                $_ab = ["A", "B"];
                $_tf = ["T", "F"];
                $question->setAnswer(str_replace($_ab, $_tf, $question->getAnswer()));
            }
            if (strlen($question->getAnswer()) > 1) {
                $question->setAnswer(implode(";", str_split($question->getAnswer())));
            }
            $data["record"][] = [$question->getType(), "", $question->getStem(), $question->getOperation(), $question->getAnswer(), "", $question->getLevel(), $question->getScore(), $courseCode];
        }
    }
    return $data;

}